home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / GLOBBINGLEXICON.PY < prev    next >
Encoding:
Python Source  |  2000-08-23  |  8.8 KB  |  281 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64.  
  65. import string, regex, ts_regex
  66. import regsub
  67.  
  68. from Lexicon import Lexicon
  69.  
  70.  
  71. __doc__=""" Lexicon object that supports 
  72.  
  73. """
  74.  
  75. from Splitter import Splitter
  76. from Persistence import Persistent
  77. from Acquisition import Implicit
  78. import OIBTree, BTree, IOBTree
  79. from intSet import intSet
  80. OIBTree=OIBTree.BTree
  81. OOBTree=BTree.BTree
  82. IOBTree=IOBTree.BTree
  83. import re
  84.  
  85. from UnTextIndex import Or
  86.  
  87. class GlobbingLexicon(Lexicon):
  88.     """
  89.  
  90.     Base class to support globbing lexicon object.
  91.     """
  92.  
  93.     multi_wc = '*'
  94.     single_wc = '?'
  95.     eow = '$'
  96.  
  97.     def __init__(self):
  98.  
  99.         self.counter = 0
  100.         self._lexicon = OIBTree()
  101.         self._inverseLex = IOBTree()
  102.         self._digrams = OOBTree()
  103.  
  104.     def set(self, word):
  105.         """  """
  106.  
  107.         if self._lexicon.has_key(word):
  108.             return self._lexicon[word]
  109.  
  110.         else:
  111.             word = intern(word)
  112.             self._lexicon[word] = self.counter
  113.             self._inverseLex[self.counter] = word
  114.  
  115.             ## now, split the word into digrams and insert references
  116.             ## to 'word' into the digram object.  The first and last
  117.             ## digrams in the list are specially marked with $ to
  118.             ## indicate the beginning and end of the word
  119.  
  120.             digrams = []
  121.             digrams.append(self.eow + word[0]) # mark the beginning
  122.  
  123.             for i in range(len(word)):
  124.                 digrams.append(word[i:i+2])
  125.  
  126.             digrams[-1] = digrams[-1] + self.eow  # mark the end
  127.  
  128.             _digrams = self._digrams
  129.             
  130.             for digram in digrams:
  131.                 set = _digrams.get(digram)
  132.                 if set is None:
  133.                     _digrams[digram] = set = intSet()
  134.                     
  135.                 set.insert(self.counter)
  136.  
  137.             self._digrams = _digrams
  138.             counter = self.counter
  139.             self.counter = self.counter + 1
  140.             return counter
  141.  
  142.  
  143.     def get(self, pattern):
  144.         """ Query the lexicon for words matching a pattern.
  145.  
  146.         """
  147.  
  148.         wc_set = [self.multi_wc, self.single_wc]
  149.         digrams = []
  150.         for i in range(len(pattern)):
  151.  
  152.             if pattern[i] in wc_set:
  153.                 continue
  154.  
  155.             if i == 0:
  156.                 digrams.insert(i, (self.eow + pattern[i]) )
  157.                 digrams.append((pattern[i] + pattern[i+1]))
  158.             else:
  159.                 try:
  160.                     if pattern[i+1] not in wc_set:
  161.                         digrams.append( pattern[i] + pattern[i+1] )
  162.  
  163.                 except IndexError:
  164.                     digrams.append( (pattern[i] + self.eow) )
  165.  
  166.  
  167.  
  168.         ## now get all of the intsets that contain the result digrams
  169.  
  170.         result = None
  171.         for digram in digrams:
  172.             if self._digrams.has_key(digram):
  173.                 set = self._digrams[digram]
  174.                 if set is not None:
  175.                     if result is None:
  176.                         result = set
  177.                     else:
  178.                         result.intersection(set)
  179.  
  180.         if result is None:
  181.             return ()
  182.         else:
  183.             ## now we have narrowed the list of possible candidates
  184.             ## down to those words which contain digrams.  However,
  185.             ## some words may have been returned that match digrams,
  186.             ## but do not match 'pattern'.  This is because some words
  187.             ## may contain all matching digrams, but in the wrong
  188.             ## order.
  189.  
  190.             expr = re.compile(self.translate(pattern))
  191.             words = []
  192.             hits = []
  193.             for x in result:
  194.                 if expr.search(self._inverseLex[x]):
  195.                     hits.append(x)
  196.  
  197.             return hits
  198.                 
  199.     def __getitem__(self, word):
  200.         """ """
  201.         return self.get(word)
  202.  
  203.     def query_hook(self, q):
  204.         """expand wildcards
  205.  
  206.         """
  207.         words = []
  208.         for w in q:
  209.             if ( (self.multi_wc in w) or
  210.                 (self.single_wc in w) ):
  211.                 wids = self.get(w)
  212.                 for wid in wids:
  213.                     if words:
  214.                         words.append(Or)
  215.                     words.append(self._inverseLex[wid])
  216.             else:
  217.                 words.append(w)
  218.  
  219.         return words
  220.  
  221.     def Splitter(self, astring, words=None):
  222.         """ wrap the splitter """
  223.  
  224.         ## don't do anything, less efficient but there's not much
  225.         ## sense in stemming a globbing lexicon.
  226.  
  227.         return Splitter(astring)
  228.  
  229.  
  230.     def translate(self, pat):
  231.         """Translate a PATTERN to a regular expression.
  232.  
  233.         There is no way to quote meta-characters.
  234.         """
  235.  
  236.         i, n = 0, len(pat)
  237.         res = ''
  238.         while i < n:
  239.             c = pat[i]
  240.             i = i+1
  241.             if c == self.multi_wc:
  242.                 res = res + '.*'
  243.             elif c == self.single_wc:
  244.                 res = res + '.'
  245.             else:
  246.                 res = res + re.escape(c)
  247.         return res + "$"
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.